using Db4objects.Db4o.Linq;
using SharedObjects;

namespace TheGameDatabaseTool
{
	public partial class DatabaseMainScript
	{
		//to be used for migrating objects in the database
		public void Migration()
		{
			#region Change agent
			//ex.: Change Agent:ComputerSystems to Agent::ComputerSystemIds:
			//		var oldAgents = (from Agent a in db select a);
			//		foreach(Agent a in oldAgents)
			//		{
			//		    if(a.ComputerSystemIds == null)
			//		        a.ComputerSystemIds = new List<ulong>();
			//		    foreach(ComputerSystem cs in a.ComputerSystems)
			//		    {
			//		        a.ComputerSystemIds.Add(cs.ID);
			//		    }
			//		    a.ComputerSystems = null; //remove data from db
			//		    db.PersistEntity(a);	  //and store
			//		}
			//		db.Commit();
			#endregion

			#region Change relation direction
			//ex.: Change top-down to bottom-up reference, ex. for SystemFile
			//1.) introduce ParentDirectoryId to SystemFile object
			//2.) fill field with appropriate data
			//3.) store updated SystemFile objects
			//4.) remove SystemFile reference from parent directory
			//5.) store parent directory to remove the data from the db.

			//		var directories = (from SystemDirectory d in db select d);
			//		foreach(SystemDirectory d in directories)
			//		{
			//		    foreach(SystemFile f in d.Files)
			//		    {
			//		        f.ParentDirectoryId = d.ID;
			//		        db.Store(f);
			//		    }
			//		    d.Files = null;
			//		    db.PersistEntity(d);
			//		}
			//		db.Commit();
			//7.) change SystemDirectory object to remove reference collection
			#endregion


			//var agents = (from Agent a in db where a.EmailAccounts != null && a.EmailAccounts.Count != 0 select a).ToList();
			//foreach(Agent a in agents)
			//{
			//    if(a.EmailAccounts.Count == 0)
			//        continue;
			//    UsenetAccount ua = new UsenetAccount {ID = Guid.NewGuid().ToLong(), AccountStatus = UsenetAccountStatus.Active, EmailAccountId = a.EmailAccounts[0].ID, IsAdmin = false};
			//    a.UsenetAccount = ua;
			//    db.PersistEntity(a);
			//}
			//db.Commit();
			var posts = (from UsenetMessage msg in Db select msg);
			foreach(var p in posts)
				Db.Delete(p);
			Db.Commit();
			if (OperationCompleted != null)
				OperationCompleted(this, null);
		}
	}
}